package com.datamigration;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

import com.bss.GenericDAO;
import com.bss.model.Relationship;
import com.oldbss.beans.AttendentRecord;
import com.oldbss.beans.CheckInOut;
import com.oldbss.beans.PatientRecord;
import com.oldbss.beans.Paymentsmade;
import com.oldbss.beans.RoomAllocation;
import com.oldbss.beans.Rooms;

public class BssOld {
	private EntityManager entityManager;

	public BssOld() {

		EntityManagerFactory emf = Persistence
				.createEntityManagerFactory("bssOld");
		this.entityManager = (EntityManager) emf.createEntityManager();
	}

	public EntityManager getEntityManager() {
		return this.entityManager;
	}

	@SuppressWarnings("rawtypes")
	public void findAll(GenericDAO genericDao) {
		genericDao.setEm(getEntityManager());
		List<?> list = genericDao.findAll();
		System.out.println(list.size());
	}

	@SuppressWarnings("unchecked")
	public List<PatientRecord> findAllPatientRecords() {
		return this.entityManager.createQuery(
				"from PatientRecord order by patientId").getResultList();
	}

	@SuppressWarnings("unchecked")
	public List<CheckInOut> findAllCheckInOut(int patientId) {
		return this.entityManager.createQuery(
				"from CheckInOut where patient.patientId="+patientId).getResultList();
	}

	@SuppressWarnings("unchecked")
	public List<String> findAllMedicalProblems() {
		return this.entityManager.createQuery(
				"select distinct medicalProblem from PatientRecord")
				.getResultList();
	}

	@SuppressWarnings("unchecked")
	public List<String> findAllIdCardType() {
		List<String> idCardTypes = this.entityManager.createQuery(
				"select distinct idCardType from PatientRecord")
				.getResultList();
		idCardTypes.addAll(this.entityManager.createQuery(
				"select distinct idType from AttendentRecord").getResultList());
		return idCardTypes;
	}

	@SuppressWarnings("unchecked")
	public List<String> findAllprofession() {
		return this.entityManager.createQuery(
				"select distinct profession from PatientRecord")
				.getResultList();
	}

	@SuppressWarnings("unchecked")
	public List<String> findAllAllocationType() {
		return this.entityManager.createQuery(
				"select distinct allocationType from UtensilAllocation")
				.getResultList();
	}

	@SuppressWarnings("unchecked")
	public List<Object[]> findAllBuildingInfo() {
		return this.entityManager
				.createQuery(
						"select distinct buildingName,Max(floorNo) from Rooms group by buildingName")
				.getResultList();
	}

	@SuppressWarnings("unchecked")
	public List<Rooms> findAllRoomsByBuildingName(String buildingName) {
		return (List<Rooms>) this.entityManager.createQuery(
				"from Rooms where buildingName='" + buildingName
						+ "' order by roomId").getResultList();

	}

	@SuppressWarnings("unchecked")
	public List<String> findAllRelationships() {
		return (List<String>) this.entityManager.createQuery(
				"select distinct relationship from AttendentRecord")
				.getResultList();
	}

	@SuppressWarnings("unchecked")
	public List<CheckInOut> findAllCheckInOuts() {
		return this.entityManager.createQuery(
				"from CheckInOut order by transactionId").getResultList();
	}

	public List<AttendentRecord> findAllAttendentRecordsByPatientId(
			int patientId) {
		return this.entityManager.createQuery(
				"from AttendentRecord where patientId=" + patientId)
				.getResultList();
	}

	public List<Paymentsmade> findAllPaymentsMade() {
		return this.entityManager.createQuery("from Paymentsmade")
				.getResultList();
	}

	public List<RoomAllocation> findAllOldRoomAllocations() {
		return this.entityManager.createQuery("from RoomAllocation")
		.getResultList();
	}

}
